package com.mybatispuls.springboot.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatispuls.springboot.Utils.R;
import com.mybatispuls.springboot.entity.EduOrder;
import com.mybatispuls.springboot.entity.Excel.OrderExcel;
import com.mybatispuls.springboot.entity.Vo.OrderVo;
import com.mybatispuls.springboot.service.impl.EduOrderServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-29
 */
@RestController
@RequestMapping("/springboot/order")
@CrossOrigin
public class EduOrderController {

    @Autowired
    private EduOrderServiceImpl eduOrderService;

    //根据id查询
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        EduOrder order = eduOrderService.getById(id);
        return R.ok().data("order",order);
    }
    //删除
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id){
        eduOrderService.removeById(id);
        return R.ok();
    }
    //修改
    @PostMapping("insert")
    public R insert(@RequestBody EduOrder eduOrder){
        eduOrderService.updateById(eduOrder);
        return R.ok();
    }

    //带查询的分页
    @PostMapping("selectPage/{current}/{limit}")
    public R selectPage(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) OrderVo orderVo){
        Page<EduOrder> Page = new Page<>(current,limit);
        QueryWrapper<EduOrder> wrapper = new QueryWrapper<>();
        String username = orderVo.getUsername();
        String bookname = orderVo.getBookname();
        String author = orderVo.getAuthor();
        if (!StringUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        if (!StringUtils.isEmpty(bookname)){
            wrapper.like("bookname",bookname);
        }
        if (!StringUtils.isEmpty(author)){
            wrapper.like("author",author);
        }
        //根据时间排序
        wrapper.orderByDesc("gmt_create");
        eduOrderService.page(Page, wrapper);
        long total = Page.getTotal();
        List<EduOrder> records = Page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //数据导出
    @PostMapping("OrderExcel")
    public R OrderExcel(){
        String filename = "D:\\image\\order.xlsx";
        List<EduOrder> list = eduOrderService.list(null);
        ArrayList<OrderExcel> object = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            EduOrder eduOrder = list.get(i);
            OrderExcel orderExcel = new OrderExcel();
            BeanUtils.copyProperties(eduOrder,orderExcel);
            object.add(orderExcel);
        }
        EasyExcel.write(filename,OrderExcel.class).sheet("订单列表").doWrite(object);
        return R.ok();
    }

}

